using System;
using System.Collections.Generic;
using System.Linq;
namespace WebClient.Models.Repositories
{
	#region using
	using WebClient.Models;
	using WebClient.Helpers;
	using System.Web.Security;
	using WebClient.Models.ViewModels;
    using WebClient.CF_DAL_WS;
	#endregion
	public class UsersRepository
	{
		public int TotalItem { get; set; }
		
		public List<UsersDTO> SelectPaging(int page, int pageSize)
		{
            List<UsersDTO> lstItem = new List<UsersDTO>();
            var userCollection = DALService.Instance.Users_SelectAll();
			foreach (var user in userCollection)
			{
                var roleForUser = user.Role;
                if (roleForUser.Name.ToUpper() != "ADMIN" && roleForUser.Name.ToUpper() != "MANAGER")
                {
                    var userInfo = new UsersDTO
                    {
                        ID = (Guid)user.ID,
                        UserName = user.UserName,
                        Email = user.Email,
                        Role = roleForUser
                    };
                    lstItem.Add(userInfo);
                }
			}
			TotalItem = lstItem.Count;
            if (pageSize > 0)
            {
                int startIndex, itemCount;
                startIndex = (page - 1) * pageSize;
                itemCount = pageSize;
                if (startIndex + itemCount > lstItem.Count)
                    itemCount = lstItem.Count - startIndex;
                lstItem = lstItem.GetRange(startIndex, itemCount);
            }

			return lstItem;
		}

        public UsersDTO SelectByUserName(string username)
		{
            return DALService.Instance.Users_SelectAll().FirstOrDefault(n => n.UserName == username);
		}

		public UsersDTO SelectByID(Guid id)
		{
            var item = DALService.Instance.Users_SelectByID(id.ToString());
			if (item != null)
				return item;
            return null;
		}

        public List<UsersDTO> SelectManager(int idrestaurant)
        {
            var restaurant = DALService.Instance.Restaurant_SelectByID(idrestaurant.ToString());
            Guid idManager;
            if ((idManager = restaurant.IDManager) != null)
            {
                return DALService.Instance.Users_RetrieveByID(idManager.ToString()).ToList();
            }         
            return new List<UsersDTO>();
        }

		public List<UsersDTO> RetrieveByID(Guid id)
		{
            var lst = new List<UsersDTO>();
            var item = DALService.Instance.Users_SelectByID(id.ToString());
            var baseinfo = new UsersDTO
			{
                ID = item.ID,
                UserName = item.UserName,
                Email = item.Email,
                Role = item.Role
			};
            if (item != null)
            {
                lst.Add(item);
            }
			return lst;
		}

        public List<UsersDTO> RetrieveByUserName(string username)
        {
            return DALService.Instance.Users_RetrieveByUserName(username).ToList();
        }

		public bool Delete(Guid id)
		{
			try
			{
                return DALService.Instance.Users_Delete(id.ToString());
			}
			catch
			{
				return false;
			}
		}

        public bool Update(UsersDTO obj)
        {
            try
            {
                return DALService.Instance.Users_Update(obj);
            }
            catch
            {
                return false;
            }
        }

		public string Insert(AddUsersViewModel obj)
		{
            return DALService.Instance.Users_Insert(obj.UserName, obj.Password, obj.Email, obj.Role.Name, obj.Name, obj.Phone, obj.Address);
		}

		public int GetTotalItem(string role)
		{
			int count = 0;
			MembershipUserCollection userCollection = Membership.GetAllUsers();
			foreach (MembershipUser user in userCollection)
			{
				var roleForUser = Roles.GetRolesForUser(user.UserName)[0];
                if (role.ToUpper() == "ADMIN")
				{
                    if (roleForUser.ToUpper() != "ADMIN")
					{
						count++;
					}
				}
			}
			return count;
		}

		public int GetTotalPage(string role)
		{
			int RecordCount = GetTotalItem(role);
			int PageSize = WebConfiguration.NumUsersPerPage;
			return (RecordCount / PageSize) + ((RecordCount % PageSize == 0) ? 0 : 1);
		}

        public bool ChangePassword(string userName, string oldpassword, string newpassword)
        {
            try
            {
                return DALService.Instance.Users_ChangePassword(userName, oldpassword, newpassword);
            }
            catch
            {
                return false;
            }
        }

        public string ResetPassword(string userName, string email)
        {
            try
            {
                return DALService.Instance.Users_ResetPassword(userName, email);
            }
            catch
            {
                return string.Empty;
            }
        }
	}
}